//
//  SettingTableController.m
//  GIFShow
//
//  Created by lanou11air029 on 15/4/22.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "SettingTableController.h"
#import "MySettingOptions.h"
#import "TextFontSetCell.h"
#import "themeCell.h"
#import "ClassSetWithCell.h"
#import "TextColorCell.h"
#import "ClassLabelModel.h"
#import "ClassSettingView.h"
#import "MySettingOptions.h"
@interface SettingTableController ()<UIPickerViewDataSource,UIPickerViewDelegate,UIActionSheetDelegate>
@property (nonatomic) NSMutableArray * heightArray;
@property (nonatomic, strong) NSMutableArray * array;
@property (nonatomic, strong) NSArray * colorArray;
@property (nonatomic, strong) NSArray * colorTitles;
@property (nonatomic) MySettingOptions * options;
@property (nonatomic, strong) NSMutableArray * totalArray;
@property (nonatomic, strong) NSMutableArray * sourceArray;

@property (nonatomic, strong) NSMutableArray * likeArray;

@end

@implementation SettingTableController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tableView.scrollEnabled = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[TextColorCell class] forCellReuseIdentifier:@"textColorCell"];
    [self.tableView registerClass:[TextFontSetCell class] forCellReuseIdentifier:@"textCell"];
    [self.tableView registerClass:[themeCell class] forCellReuseIdentifier:@"themeCell"];
    [self.tableView registerClass:[ClassSetWithCell class] forCellReuseIdentifier:@"classSetCell"];
    
    self.array = [NSMutableArray arrayWithObjects:@"文字颜色",@"文字大小",@"主题样式", nil];
    self.colorArray = [NSArray arrayWithObjects:[UIColor blackColor],[UIColor darkGrayColor],[UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor yellowColor], [UIColor magentaColor],[UIColor orangeColor],[UIColor purpleColor],[UIColor brownColor]  , nil];
    self.colorTitles = [NSArray arrayWithObjects:@"黑色",@"深灰色",@"红色",@"绿色",@"蓝色",@"黄色",@"品红色",@"橙色",@"紫色",@"棕色", nil];
    // Uncomment the following line to preserve selection between presentations.
     self.clearsSelectionOnViewWillAppear = NO;
    self.tableView.allowsMultipleSelection = NO;

    [self myTheme];
    [[NSNotificationCenter defaultCenter]addObserverForName:@"ChangeTheme" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        [self myTheme];
    }];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


}
-(void)myTheme{
    NSString * file = [MySettingOptions defaultOptions].backColor;
    if(self.view.frame.size.width == 375){
        NSString * back = [NSString stringWithFormat:@"%@6",file];
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:back]];
    }else{
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:file]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0){
        TextColorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textColorCell" forIndexPath:indexPath];

        return cell;
    }
    
    if (indexPath.row == 1) {
        TextFontSetCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        textCell.textTitleLabel.text = self.array[indexPath.row];
        [textCell.textStepper addTarget:self action:@selector(changeTextFontValue:) forControlEvents:UIControlEventValueChanged];
        return textCell;
    }
    
    if (indexPath.row == 2) {
        themeCell * themeCell = [tableView dequeueReusableCellWithIdentifier:@"themeCell" forIndexPath:indexPath];
        
        return themeCell;
    }
    if (indexPath.row == 3) {
        ClassSetWithCell * clsCell = [tableView dequeueReusableCellWithIdentifier:@"classSetCell" forIndexPath:indexPath];

        return clsCell;
    }

    

    // Configure the cell...
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.heightArray) {
        self.heightArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 9; i++) {
            NSNumber * height = [NSNumber numberWithFloat:self.view.frame.size.height*0.085];
            [self.heightArray addObject:height];
        }
    }
    return [self.heightArray[indexPath.row + indexPath.section*3] floatValue];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return self.view.frame.size.height* 0.03;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != 0) {
        CGFloat cellHeight = 0;
        if (indexPath.row == 1){
            cellHeight = self.view.bounds.size.height * 0.19+10;
        }
        if (indexPath.row == 2) {
            cellHeight = self.view.bounds.size.height * 0.34;
        }
        
            [self.heightArray replaceObjectAtIndex:indexPath.row+indexPath.section*3 withObject:[NSNumber numberWithFloat:cellHeight]];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
if (indexPath.row == 0) {

    [self.heightArray replaceObjectAtIndex:indexPath.row+indexPath.section*3 withObject:[NSNumber numberWithFloat:self.view.frame.size.height*0.085+20]];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(500 * USEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self tableView:tableView didDeselectRowAtIndexPath:indexPath];
    });
        [self configColors];
    }
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.heightArray replaceObjectAtIndex:indexPath.row+indexPath.section*3 withObject:[NSNumber numberWithFloat:self.view.frame.size.height*0.085]];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
}

#pragma mark configTextFont
-(void)changeTextFontValue:(UIStepper *)stepper{
    NSIndexPath * secondIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    TextFontSetCell * textCell = (TextFontSetCell *)[self.tableView cellForRowAtIndexPath:secondIndexPath];
    textCell.instanceLabel.text = [NSString stringWithFormat:@"%@：%d",@"示例",(int)stepper.value];
    textCell.instanceLabel.font = [UIFont systemFontOfSize:stepper.value];
    [MySettingOptions defaultOptions].titleFontValue = stepper.value;
    [MySettingOptions defaultOptions].userFontValue = stepper.value+1;
    [MySettingOptions defaultOptions].contentFontValue = stepper.value - 3;
    [MySettingOptions defaultOptions].otherFontValue = stepper.value - 6;
}

#pragma  mark configColor
-(void)configColors{
    CGRect frame = self.view.bounds;
    frame.origin = CGPointMake(0, 400);
    UIView * lightView = [[UIView alloc]initWithFrame:self.view.bounds];
    lightView.alpha = 0.5;
    CAGradientLayer * layer = [self gradientLayer];
    [lightView.layer addSublayer:layer];
    UIPickerView * pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-250, self.view.frame.size.width, 250)];
    pickView.dataSource = self;
    pickView.delegate = self;
    pickView.showsSelectionIndicator = YES;

    [lightView addSubview:pickView];
    frame.origin = CGPointMake(0, 0);
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removePickView:)];
    lightView.tag = 10000;
    [lightView addGestureRecognizer:tap];
    UILabel * confirmLabel = [[UILabel alloc]initWithFrame:CGRectMake(pickView.frame.origin.x+10, pickView.frame.origin.y-50, pickView.frame.size.width-20, 40)];
    confirmLabel.layer.cornerRadius = 10;
    confirmLabel.layer.borderColor = [UIColor colorWithRed:0.5 green:0.6 blue:0.9 alpha:0.9].CGColor;
    confirmLabel.layer.borderWidth = 1;
    confirmLabel.textColor = [UIColor blueColor];
    confirmLabel.text = @"完 成";
    confirmLabel.textAlignment = NSTextAlignmentCenter;
    confirmLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
    confirmLabel.clipsToBounds = YES;
    [lightView addSubview:confirmLabel];
    [UIView animateWithDuration:0.7 animations:^{
        lightView.alpha = 1;
        lightView.frame = frame;
        [self.view addSubview:lightView];

        
    }];
    self.tableView.scrollEnabled = NO;
//    [self presentViewController:colorNVC animated:YES completion:^{
//
//    }];
}
-(void)removePickView:(UITapGestureRecognizer *)tap{
    if(tap.view.tag == 10000){
        [UIView animateWithDuration:0.6 animations:^{
            tap.view.frame = CGRectMake(0, 400, self.view.frame.size.width, self.view.frame.size.height);
        } completion:^(BOOL finished) {
            [tap.view removeFromSuperview];
        }];
    }
    self.tableView.scrollEnabled = YES;
}
#pragma mark - PickView
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    /*
     + (UIColor *)blackColor;      // 0.0 white
     + (UIColor *)darkGrayColor;   // 0.333 white
     + (UIColor *)redColor;        // 1.0, 0.0, 0.0 RGB
     + (UIColor *)greenColor;      // 0.0, 1.0, 0.0 RGB
     + (UIColor *)blueColor;       // 0.0, 0.0, 1.0 RGB
     + (UIColor *)yellowColor;     // 1.0, 1.0, 0.0 RGB
     + (UIColor *)magentaColor;    // 1.0, 0.0, 1.0 RGB
     + (UIColor *)orangeColor;     // 1.0, 0.5, 0.0 RGB
     + (UIColor *)purpleColor;     // 0.5, 0.0, 0.5 RGB
     + (UIColor *)brownColor;      // 0.6, 0.4, 0.2 RGB
     */
    return self.colorArray.count;
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [[NSAttributedString alloc]initWithString:self.colorTitles[row] attributes:@{NSForegroundColorAttributeName:self.colorArray[row]}];
}
// attributed title is favored if both methods are implemented
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UIView * reView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    NSAttributedString * attributeStr = [self pickerView:pickerView attributedTitleForRow:row forComponent:component];


    UIView * colorView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 4.0-25, 0, 50, 40)];
    colorView.backgroundColor = self.colorArray[row];
    [reView addSubview:colorView];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(colorView.frame.origin.x + colorView.frame.size.width + 50, 0, 150, 40)];
    label.attributedText = attributeStr;
    [reView addSubview:label];

    return reView;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    TextColorCell * cell = (TextColorCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    cell.textTitleLabel.textColor = self.colorArray[row];
    cell.colorView.backgroundColor = self.colorArray[row];
    self.options.foreGColor = self.colorArray[row];
    self.options.myFontColorValue = row;
    [[NSUserDefaults standardUserDefaults] setInteger:self.options.myFontColorValue forKey:@"fontColor"];
    [[NSUserDefaults standardUserDefaults] synchronize];
//    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}
-(CAGradientLayer *)gradientLayer{
    CAGradientLayer * layer = [[CAGradientLayer alloc]init];
    layer.frame = self.view.bounds;
    layer.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.5 alpha:0.5].CGColor,(id)[UIColor whiteColor].CGColor, nil];
    layer.locations = @[@0.5,@0.7];
    layer.startPoint = CGPointMake(0, 0);
    layer.endPoint = CGPointMake(0, 1);
    return layer;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
